package com.flashphoner.api
{
		
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.flashphoner.Logger;
	import com.flashphoner.api.data.Call;
	import com.flashphoner.api.data.ModelLocator;
	import com.flashphoner.api.data.PhoneConfig;
	import com.flashphoner.api.management.SoundControl;
	
	import flash.events.*;
	import flash.utils.*;
	
	internal class PhoneCommand implements ICommand
	{		
		private var hangupTimer:Timer;
		
		private var flashAPI:Flash_API;		
				
									
		public function PhoneCommand()
		{
		}	
				
		public function execute( event : CairngormEvent ) : void
		{	
			flashAPI = (event as MainEvent).flashAPI;
			Logger.info("PhoneCommand.execute() event.type "+event.type);
			if (event.type==MainEvent.TALK){
				Logger.info("MainEvent.TALK");
				var call:Call = (event as MainEvent).obj as Call;											
				Logger.info("MainEvent.TALK "+call.id);
				flashAPI.phoneServerProxy.publish(call);
				 			
				flashAPI.phoneServerProxy.phoneSpeaker.play("INCOMING_"+flashAPI.modelLocator.login+"_"+call.id);				
				if (PhoneConfig.VIDEO_ENABLED){
					flashAPI.phoneServerProxy.phoneSpeaker.playVideo("VIDEO_INCOMING_"+flashAPI.modelLocator.login+"_"+call.id);
				}
			}
			
			if (event.type == MainEvent.SESSION_PROGRESS){
				Logger.info("MainEvent.SESSION_PROGRESS");
				var call:Call = (event as MainEvent).obj as Call;
		 		flashAPI.currentCall = call;
		 		flashAPI.phoneServerProxy.publish(call);	 		
		 		flashAPI.phoneServerProxy.phoneSpeaker.play("INCOMING_"+flashAPI.modelLocator.login+"_"+call.id);
		 	}
			
			if (event.type==MainEvent.IN){
				Logger.info("MainEvent.IN");						
			}
			
			if (event.type ==MainEvent.OUT){				
				Logger.info("MainEvent.OUT");
			}
			if (event.type == MainEvent.BUSY){
				var call:Call = (event as MainEvent).obj as Call;
				finish(call);	
			}				
			
			if (event.type == MainEvent.FINISH){
				var call:Call = (event as MainEvent).obj as Call;
				finish(call);
			}			
			

			
			
			if (event.type == MainEvent.CONNECTED){				
				if (!PhoneConfig.REGISTER_REQUIRED){				
					flashAPI.modelLocator.logged = true;		
				}						
			}
			if (event.type == MainEvent.REGISTERED){
				var modelLocator:ModelLocator = flashAPI.modelLocator;
				flashAPI.modelLocator.logged = true;		
			}			
			
		}
		
		private function finish(call:Call):void {
				flashAPI.phoneServerProxy.unpublish(call);	
				flashAPI.currentCall=null;
				flashAPI.phoneServerProxy.phoneSpeaker.stop();
		}
		
	}
}